package xyz.xuminghai.javafx_learn.scene.effect;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * 2023/7/3 16:46 星期一<br/>
 * <p>
 * 透视变换能够将任意四边形映射到另一个任意四边形，同时保持直线的直线度
 *
 * @author xuMingHai
 */
public class PerspectiveTransformApp extends Application {

    private final BorderPane root = new BorderPane();

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        PerspectiveTransform perspectiveTransform = new PerspectiveTransform();
        perspectiveTransform.setUlx(10.0);
        perspectiveTransform.setUly(10.0);
        perspectiveTransform.setUrx(310.0);
        perspectiveTransform.setUry(40.0);
        perspectiveTransform.setLrx(310.0);
        perspectiveTransform.setLry(60.0);
        perspectiveTransform.setLlx(10.0);
        perspectiveTransform.setLly(90.0);
        Group group = new Group();
        group.setEffect(perspectiveTransform);
        group.setCache(true);
        Rectangle rect = new Rectangle();
        rect.setX(10.0);
        rect.setY(10.0);
        rect.setWidth(280.0);
        rect.setHeight(80.0);
        rect.setFill(Color.web("0x3b596d"));
        Text text = new Text();
        text.setX(20.0);
        text.setY(65.0);
        text.setText("Perspective");
        text.setFill(Color.ALICEBLUE);
        text.setFont(Font.font(null, FontWeight.BOLD, 36));
        group.getChildren().addAll(rect, text);

        root.setCenter(group);
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
